//
//  ZJOfflineStudyMapViewViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/7/21.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJOfflineStudyMapViewViewController.h"
/** 地图 */
#import "QMapKit.h"
#import "ZJPointAnnotation.h"
#import "ZJCommunitySchoolModel.h"
#import "ZJOfflineStudyViewController.h"
@interface ZJOfflineStudyMapViewViewController ()<QMapViewDelegate>
@property (nonatomic, strong) QMapView *mapView;
/** 标记点数组 */
@property (nonatomic, strong) NSMutableArray *annotationArray;

@end

@implementation ZJOfflineStudyMapViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"线下学";
    [self.view addSubview:self.mapView];
    [self networking_nearbyOrganization];
    //    31.226894,121.497116 上海市坐标
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.mapView setCenterCoordinate:DEFLocation animated:YES];
    });
    
}


//坐标点
- (void)networking_nearbyOrganization{
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getSchoolList] Parameter:nil Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            weakSelf.annotationArray = [[NSArray yy_modelArrayWithClass:[ZJCommunitySchoolModel class] json:[object objectForKey:@"SCHOOL_LIST"]] mutableCopy];
            [weakSelf annotationArrayUpdate];
        }
    } Fail:^(id object) {
        
    }];
}



- (void)annotationArrayUpdate{
    for (QPointAnnotation *annotation in self.mapView.annotations) {
        if ([annotation isKindOfClass:[ZJPointAnnotation class]]) {
            [self.mapView removeAnnotation:annotation];
        }
    }
    
    for (int i = 0; i < self.annotationArray.count; i++) {
        ZJPointAnnotation* anno = [[ZJPointAnnotation alloc] init];
        id model1 = self.annotationArray[0];
        if ([model1 isKindOfClass:[ZJCommunitySchoolModel class]]) {
            ZJCommunitySchoolModel *model = self.annotationArray[i];
            anno.coordinate = CLLocationCoordinate2DMake([model.POINT_X doubleValue],[model.POINT_Y doubleValue]);
            anno.index = i;
            [self.mapView addAnnotation:anno];
            
        }
    }
}


//设置大头针

- (QAnnotationView *)mapView:(QMapView *)mapView
           viewForAnnotation:(id<QAnnotation>)annotation
{
    //设置复用标识
    static NSString *pointReuseIdentifier = @"pointReuseIdentifier";
    QAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIdentifier];
    if (annotationView == nil) {
        annotationView = [[QAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIdentifier];
    }
    //显示气泡
    [annotationView setCanShowCallout:YES];
    
    if ([annotation isKindOfClass:[ZJPointAnnotation class]]) {
        //设置图标
        [annotationView setImage:[UIImage imageNamed:@"map_zhengdi"]];
    }
    return annotationView;
    
}
//点击大头针走的方法（根据View里面的annotation的index值判断）
- (void)mapView:(QMapView *)mapView didSelectAnnotationView:(QAnnotationView *)view{
    if ([view.annotation isKindOfClass:[ZJPointAnnotation class]]) {
        ZJPointAnnotation *annotation = view.annotation;
        //选中状态设置NO。可以连续点击
        [view setSelected:NO];
        ZJOfflineStudyViewController *vc = [ZJOfflineStudyViewController new];
        vc.model = self.annotationArray[annotation.index];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - Setter


#pragma mark - Getters

- (QMapView *)mapView{
    if (_mapView == nil){
        _mapView = [[QMapView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight)];
        _mapView.showsUserLocation = YES;//显示用户位置
        _mapView.showsCompass = NO;//不显示指南针
        _mapView.userTrackingMode = QMUserTrackingModeFollow;
        _mapView.zoomLevel = 16;
        _mapView.delegate = self;
        _mapView.disable3DBuildingEffect = YES;
    }
    return _mapView;
}

- (NSMutableArray *)annotationArray{
    if (_annotationArray == nil){
        _annotationArray = [NSMutableArray array];
    }
    return _annotationArray;
}

@end
